package com.zghhost.codedemo.net.socket;

import java.io.*;
import java.net.Socket;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/3/29 15:23
 */
public class MySocketClient {
    public static void main(String[] args)throws Exception{
        Socket client = new Socket("127.0.0.1",1234);
        System.out.println("我是一个客户端,我要连接1234端口："+client.getLocalAddress()+":"+client.getLocalPort());
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
        new Thread(() -> {
            try{
                String mline = null;
                while((mline = br.readLine()) != null){
                    System.out.println(mline);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
        System.out.println("连接成功,你可以在控制台说话了");
        BufferedReader cbr = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while((line = cbr.readLine()) != null){
            bw.write(line);
            bw.write("\n");
            bw.flush();
        }
    }
}
